﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Audio;
using System.IO;
using System.Threading;
using System.IO.IsolatedStorage;

namespace Windows_Phone_Audio_Recorder
{
    public partial class MainPage : PhoneApplicationPage
    {
        MemoryStream m_msAudio = new MemoryStream();
        Microphone m_micDevice = Microphone.Default;
        byte[] m_baBuffer;
        SoundEffect m_sePlayBack;
        ViewModel vm = new ViewModel();
        long m_lDuration = 0;
        bool m_bStart = false;
        bool m_bPlay = false;

        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
            this.DataContext = vm;
           
            //icBar.ItemsSource = vm.AudioData;
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            m_bStart = true;
            tbData.Text = "00:00:00";
            m_lDuration = 0;
            m_micDevice.BufferDuration = TimeSpan.FromMilliseconds(1000);
            m_baBuffer = new byte[m_micDevice.GetSampleSizeInBytes(m_micDevice.BufferDuration)];
            m_micDevice.BufferReady +=new EventHandler(m_Microphone_BufferReady);
            m_micDevice.Start();
        }

        void m_Microphone_BufferReady(object sender, EventArgs e)
        {
            m_micDevice.GetData(m_baBuffer);
            this.Dispatcher.BeginInvoke(() =>
                {
                    vm.LoadAudioData(m_baBuffer);
                    m_lDuration++;
                    TimeSpan tsTemp = new TimeSpan(m_lDuration * 10000000);
                    tbData.Text = tsTemp.Hours.ToString().PadLeft(2, '0') + ":" + tsTemp.Minutes.ToString().PadLeft(2, '0') + ":" + tsTemp.Seconds.ToString().PadLeft(2, '0');
                }
                );
            //this.Dispatcher.BeginInvoke(new Action(() => vm.LoadAudioData(m_baBuffer))); 
            //this.Dispatcher.BeginInvoke(new Action(() => tbData.Text = m_baBuffer[0].ToString() + m_baBuffer[1].ToString() + m_baBuffer[2].ToString() + m_baBuffer[3].ToString()));
            m_msAudio.Write(m_baBuffer,0, m_baBuffer.Length);
        }
       
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (m_bStart)
            {
                m_bStart = false;
                m_micDevice.Stop();
                ProgressPopup.IsOpen = true;
            }

            if (m_bPlay)
            {
                m_bPlay = false;
                m_sePlayBack.Dispose();
            }

        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            m_bPlay = true;
            m_sePlayBack = new SoundEffect(m_msAudio.ToArray(), m_micDevice.SampleRate, AudioChannels.Mono);
            m_sePlayBack.Play();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtAudio.Text != "")
            {
                IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
                string strSource = txtAudio.Text;
                int nIndex = 0;
                while (isfData.FileExists(txtAudio.Text))
                {
                    strSource = txtAudio.Text + nIndex.ToString().PadLeft(2, '0') + ".wav";
                }

                IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(txtAudio.Text, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
                isfStream.Write(m_msAudio.ToArray(), 0, m_msAudio.ToArray().Length);
                isfStream.Close();
            }
            this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = false));
        }
    }
}